package cubelabs.nexus.utils;

import java.io.IOException;

import com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;

import cubelabs.nexus.domain.NexusFileUpload;

public class FileUploadUtils {

	
	public static String uploadFileToAmazon(NexusFileUpload nexusFileUpload, String counselEventId, String date) throws IOException{
		AmazonS3 amazonS3 = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider());
		//Region usWest2 = Region.getRegion(Regions.US_EAST_1);
		String bucketName = "counselme";
		ObjectMetadata objectMetadata = new ObjectMetadata();
		objectMetadata.setContentLength(nexusFileUpload.getMultiPartFile().getSize());
		PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, counselEventId + "_" + date + "_" + nexusFileUpload.getMultiPartFile().getOriginalFilename(), nexusFileUpload.getMultiPartFile().getInputStream(), objectMetadata);
		
		amazonS3.putObject(putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead));
		
		String returnUrl = "https://s3.amazonaws.com/"+bucketName+"/"+counselEventId + "_" + date + "_" +nexusFileUpload.getMultiPartFile().getOriginalFilename();
		
		return returnUrl;
	}
	
	public static String uploadOrgLogoToAmazon(NexusFileUpload nexusFileUpload, String orgId, String date) throws IOException{
		AmazonS3 amazonS3 = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider());
		//Region usWest2 = Region.getRegion(Regions.US_EAST_1);
		String bucketName = "counselme";
		ObjectMetadata objectMetadata = new ObjectMetadata();
		objectMetadata.setContentLength(nexusFileUpload.getMultiPartFile().getSize());
		PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, orgId + "_" + date + "_" + nexusFileUpload.getMultiPartFile().getOriginalFilename(), nexusFileUpload.getMultiPartFile().getInputStream(), objectMetadata);
		
		amazonS3.putObject(putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead));
		
		String returnUrl = "https://s3.amazonaws.com/"+bucketName+"/"+orgId + "_" + date + "_" +nexusFileUpload.getMultiPartFile().getOriginalFilename();
		
		return returnUrl;
	}
	
}
